#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

void handler(int signo){
    cout << "receive signal:" << signo << endl;
    exit(1);
}

void show(sigset_t *set){
    for ( int signo = 1; signo <= 31; signo++){
        if(sigismember(set, signo))
            cout << "1";
        else
            cout << "0";
    }
    cout << endl;
}

int main(){
    sigset_t set, oset;
    sigemptyset(&set);
    sigemptyset(&oset);
    sigaddset(&set, 2);

    sigprocmask(SIG_SETMASK, &set, &oset);
    int cnt = 0;
    while(true){
        //cout << "oset" << endl;
        show(&oset);
        //cout << "set" << endl;
        cout << "----------------" << endl;
        show(&set);
        cout << "----------------" << endl;
        sleep(1);
        cnt++;

        if(cnt == 5){
            cout << "recover block" << endl;
            sigprocmask(SIG_SETMASK, &oset, &oset);


            cout << "***************" << endl;
            show(&set);
            show(&oset);
            cout << "***************" << endl;

        }
    }

    //signal(SIGFPE, handler);

    // signal(11, handler);

    // pid_t id = fork();
    // if(id == 0){
    //     sleep(2);
    //     cout << "野指针" << endl;

    //     int *p = nullptr;
    //     *p = 100;

    //     cout << "野指针" << endl;
    //     exit(1);
    // }

    // int status = 0;
    // waitpid(id, &status, 0);
    // cout << "exit code: " << ((status >> 8) & 0xFF) << endl;
    // cout << "exit signal: " << (status & 0x7F) << endl;
    // cout << "core dump flag: " << ((status >> 7) & 0x1) << endl;

    //signal(SIGFPE, handler);
    //int a = 10;
    //a /= 0;
    //cout << "div zero" << endl;

    return 0;
}